package com.diko.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.env.Environment;

import java.util.concurrent.ThreadPoolExecutor;


/**
 * @author Emma Li
 * @date 2020/10/23 - 17:05
 */
@EnableConfigurationProperties(CustomProperties.class)
public class Lab2AutoConfig {

    @ConditionalOnProperty(prefix = "diko.auto", name = "enable",havingValue = "true")
    @Bean
    CommandLineRunner createCustomCommandLineRunner(Environment env){
        return new CustomCommandLineRunner(env);
    }


    public static class NoticeEvent extends ApplicationEvent {
        private static final Logger logger= LoggerFactory.getLogger(NoticeEvent.class);

        private final String message;

        public NoticeEvent(String message){
            super(message);
            this.message=message;

            logger.info("添加时间成功！message:{}",message);
        }

        public String getMessage(){
            return message;
        }
    }

    @Bean
    public ApplicationListener<ApplicationReadyEvent> listener() {
        return event -> System.out.print("自定义 ApplicationReadyEvent 事件监听器!");
    }
}
